﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.AspNet.Identity;
using TeamPrasioliteWarGame.Models;
using TeamPrasioliteWarGame.Controls.ErrorSuccessNotifier;

namespace TeamPrasioliteWarGame
{
    public partial class Recruite : System.Web.UI.Page
    {
        private int unitType = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["unitType"] == null)
            {
                ErrorSuccessNotifier.AddErrorMessage("Invalid unit type!");
                Response.Redirect("~/Units.aspx");
            }

            Entities context = new Entities();

            using (context)
            {
                bool tryParseUnitType = int.TryParse(Request.Params["unitType"], out unitType);

                var unit = context.GameUnits.FirstOrDefault(u => u.Id == unitType);

                if (unit != null && tryParseUnitType)
                {
                    this.LabelUnitName.Text = unit.Type;

                    string userGuidId = User.Identity.GetUserId();
                    var user = context.ExtendUsers
                                      .Include("AspNetUser")
                                      .FirstOrDefault(u => u.UserId == userGuidId);

                    int userFood = user.Food;
                    int userIron = user.Iron;

                    int availableUnits = 0;

                    while (userFood >= unit.FoodCost && userIron >= unit.IronCost)
                    {
                        userFood -= unit.FoodCost;
                        userIron -= unit.IronCost;
                        availableUnits++;
                    }

                    this.ValidatorRangeUnits.MaximumValue = availableUnits.ToString();
                    this.LabelCanRecruit.Text = availableUnits.ToString();
                }
                else
                {
                    ErrorSuccessNotifier.AddErrorMessage("Invalid unit type!");
                    Response.Redirect("~/Units.aspx");
                }
            }
        }

        protected void ButtonSubmit_Click(object sender, EventArgs e)
        {
            int unitCount = int.Parse(this.Units.Text);
            bool tryParseUnitType = int.TryParse(Request.Params["unitType"], out unitType);

            if (!tryParseUnitType)
            {
                ErrorSuccessNotifier.AddErrorMessage("Invalid unit type!");
                Response.Redirect("~/Units.aspx");
            }

            Entities context = new Entities();

            using (context)
            {
                string userGuidId = User.Identity.GetUserId();
                var user = context.ExtendUsers
                                  .Include("AspNetUser")
                                  .FirstOrDefault(u => u.UserId == userGuidId);

                var unit = user.AspNetUser.UserUnits.FirstOrDefault(u => u.UnitId == unitType);

                if (unit != null)
                {
                    unit.Count += unitCount;
                }
                else
                {
                    var gameUnit = context.GameUnits.FirstOrDefault(u => u.Id == unitType);

                    unit = new UserUnit()
                    {
                        AspNetUser = user.AspNetUser,
                        UnitId = unitType,
                        Count = unitCount,
                        GameUnit = gameUnit
                    };

                    user.AspNetUser.UserUnits.Add(unit);
                    context.SaveChanges();
                }

                user.Food -= unitCount * unit.GameUnit.FoodCost;
                user.Iron -= unitCount * unit.GameUnit.IronCost;

                context.SaveChanges();

                ErrorSuccessNotifier.AddSuccessMessage("You successfuly recruted " + unitCount + " units");
                Response.Redirect("~/Units.aspx");
            }
        }
    }
}